﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_RZConsumption : UserControl
    {
        
        public panel_RZConsumption()
        {
            InitializeComponent();
            LoadData();
            this.RZConsumption.PopupMenuShowing += RZConsumption_PopupMenuShowing;
        }

        void RZConsumption_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.rzConsumptionContext.Show(MousePosition);
        }

        //////////////////////////////////////////////
        /// Load Data to Grid View
        //////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT [RZCode], [IQID], [MassQuantity], [NetQuantity], ROUND([TieuHao],4) * 100 AS TieuHao, [IQVietnamese],"
                + " [UnitID], [UnitNameVN], [ExportRobotNo], [RobotNo], [PartNo], [RobotVietnameseName],"
                + " [Date], [1pcMassQuantity], [1pcNetQuantity], [TotalQuantity]"
                + " FROM [VConsumptionToCutoms2]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridRZConsumption.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("RZCode", typeof(string));
                newTable.Columns.Add("IQID", typeof(string));
                newTable.Columns.Add("MassQuantity", typeof(float));
                newTable.Columns.Add("NetQuantity", typeof(float));
                newTable.Columns.Add("TieuHao", typeof(float));
                newTable.Columns.Add("IQVietnamese", typeof(string));
                newTable.Columns.Add("UnitID", typeof(string));
                newTable.Columns.Add("UnitNameVN", typeof(string));
                newTable.Columns.Add("ExportRobotNo", typeof(string));
                newTable.Columns.Add("RobotNo", typeof(string));
                newTable.Columns.Add("PartNo", typeof(string));
                newTable.Columns.Add("RobotVietnameseName", typeof(string));
                newTable.Columns.Add("Date", typeof(DateTime));
                newTable.Columns.Add("1pcMassQuantity", typeof(float));
                newTable.Columns.Add("1pcNetQuantity", typeof(float));
                newTable.Columns.Add("TotalQuantity", typeof(float));
                gridRZConsumption.DataSource = newTable;
            }
            RZConsumption.Columns[0].Caption = "RZ Code";
            RZConsumption.Columns[1].Caption = "IQ ID";
            RZConsumption.Columns[2].Caption = "Mass Q'ty";
            RZConsumption.Columns[3].Caption = "Net Q'ty";
            RZConsumption.Columns[4].Caption = "Tiêu hao (%)";
            RZConsumption.Columns[5].Caption = "IQ Vietnamese";
            RZConsumption.Columns[6].Caption = "Unit";
            RZConsumption.Columns[7].Caption = "Unit Name VN";
            RZConsumption.Columns[8].Caption = "Export Robot No";
            RZConsumption.Columns[9].Caption = "Robot No";
            RZConsumption.Columns[10].Caption = "Part No";
            RZConsumption.Columns[11].Caption = "Robot Name";
            RZConsumption.Columns[12].Caption = "Date";
            RZConsumption.Columns[13].Caption = "1pc Mass Q'ty";
            RZConsumption.Columns[14].Caption = "1pc Net Q'ty";
            RZConsumption.Columns[15].Caption = "Total Q'ty";            
            RZConsumption.BestFitColumns();
        }

        private void refreshSelection_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonEport_Click(object sender, EventArgs e)
        {
            Cursor currentCursor;
            currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                SaveFileDialog saveFDlg = new SaveFileDialog();
                saveFDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                saveFDlg.Filter = "Microsoft Office Excel|*.xlsx";
                saveFDlg.Title = "Save As";
                //saveFDlg.CheckFileExists = true;
                saveFDlg.FileName = "RZConsumption.xlsx";
                if (saveFDlg.ShowDialog(this) == DialogResult.OK)
                {
                    if (!string.IsNullOrEmpty(saveFDlg.FileName))
                    {
                        RZConsumption.OptionsPrint.PrintDetails = true;
                        RZConsumption.OptionsPrint.ExpandAllDetails = true;
                        RZConsumption.OptionsPrint.AutoWidth = true;
                        RZConsumption.ExportToXlsx(saveFDlg.FileName);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Warning!");
            }
            finally
            {
                Cursor.Current = currentCursor;
            }
        }
    }
}
